#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>

#include <time.h> 
#include <sys/time.h>
#include <stdbool.h>

#include <pthread.h>
#include <signal.h>

//#include <cutils/log.h>  // only in android source
#include <android/log.h> // only use in NDK
#include "queue.h"
#include "comm.h"

#define RESULT_OK 1
#define RESULT_FALSE 0

#define TASK_DOWNLOAD_TAG  1
#define TASK_UPLOAD_TAG  2
#define TASK_REBOOT_TAG  3

static unsigned int worker_thread_task(queue_t* queue)
{
    return run_tasks(queue);
}

int run_tasks(queue_t* queue)
{
	void* data;
	int tasktype = 0;
	int result = RESULT_FALSE;
	
	while(true)
	{
		tasktype = queue_pop(queue, &data);
		if(tasktype == -1){
			//log_debug("no more task to run");
			LOGD("no more task to run");
			sleep(2);
			continue;
		}
		
		result = RESULT_OK;
		switch(tasktype)
		{
			case TASK_DOWNLOAD_TAG:
			{
				LOGD(">>>>>>>>>>>do download task.>>>>>>>>>>>");
			}
			break;
		}
	}
	
	return result;
}

int main(int argc, char **argv)
{
	int pid;
	queue_t* queue;
	int count = 0;
	
	LOGD("####main start####");
	queue = queue_create();
	
	pthread_t th1;
	pthread_create(&th1, NULL, (void*)worker_thread_task, queue);
	
	while(true){
		sleep(1);
		
		count++;
		if (count >= 10){
			queue_push(queue, NULL, TASK_DOWNLOAD_TAG);
			count = 0;
		}
	}
	
	LOGD("exit");
	return 0;
}